package com.mashibing.tank.pojo;

import com.mashibing.tank.factory.*;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TankFrame
 * @Description 画黑背景
 * @Author zhangjunhao
 * @DATE 2023/8/25 20:04
 * @Version 1.0
 */
public class TankFrame extends Frame {

    private int x = 200, y = 400;
    public static final int GAME_WIDTH = Integer.parseInt((String)PropertyMgr.get("gameWidth")),
            GAME_HEIGHT = Integer.parseInt((String)PropertyMgr.get("gameHeight"));

    public static int getGameWidth() {
        return GAME_WIDTH;
    }


    /**
     * 引用坦克对象
     * */
    private Tank myTank = new Tank(x, y, Dir.DOWN,Group.GOOD,this);

    /**
     * 引用敌方坦克对象
     * */
    private List<Tank> tanks = new ArrayList<>();

    public List<Tank> getTanks() {
        return tanks;
    }

    /**
     * 引用子弹对象
     * */
    private List<BaseBullet> myBullets = new ArrayList<>();


    /**
     * 碰撞检测对象  使用抽象工厂模式
     * */
    private List<BaseExplode> explodes = new ArrayList<>();

    public GameFactory gameFactory = new DefaultFactory();
    // public  GameFactory gameFactory = new RectFactory();

    public List<BaseExplode> getExplodes() {
        return explodes;
    }

    public List<BaseBullet> getMyBullets() {
        return myBullets;
    }
    public static int getGameHeight() {
        return GAME_HEIGHT;
    }


    /**
     * @Author zhangjunhao
     * @Description 构造方法
     * @Date  2023/8/27
     * @Param
     * @return
     **/
    public TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("Tank War");
        setVisible(true);

        //监听键盘
        this.addKeyListener(new MyKeyListener());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


    /**
     * @Author zhangjunhao
     * @Description TODO repaint()都会刷新自己paint()方法
     * @Date 2023/8/25
     **/
    @Override
    public void paint(Graphics g) {

        //为了显示是否内存泄露
        g.drawString("子弹的数量: "+ myBullets.size(),10,60);
        g.drawString("敌人坦克的数量: "+tanks.size(),10,80);
        g.drawString("爆炸的数量: "+explodes.size(),10,100);

        myTank.paint(g);


        for (int i = 0; i < myBullets.size(); i++) {
            myBullets.get(i).paint(g);
        }

        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }


        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }

        //碰撞
        for (int i = 0; i < myBullets.size(); i++) {
            for (int i1 = 0; i1 < tanks.size(); i1++) {
                myBullets.get(i).collideWith(tanks.get(i1));
            }
        }




    }




    /**
     * 窗口参数设置对象
     * */
    private Image offScreenImage = null;

    /**
     * @Description TODO 这是截取电脑的画笔,然后通过自己的调用画上去
     * @Date 2023/8/26
     **/
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.GRAY);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    /**
     * @Author zhangjunhao
     * @Description 内部类实现键盘监听
     * @Date 2023/8/25
     **/
    class MyKeyListener extends KeyAdapter {

        boolean bU = false;
        boolean bL = false;
        boolean bR = false;
        boolean bD = false;


        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            seMainTankDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    myTank.fire();
                default:
                    break;
            }

            seMainTankDir();

        }

        /**
         * @return void
         * @Author zhangjunhao
         * @Description TODO 方向移动,根据按键移动
         * @Date 2023/8/25
         * @Param null
         **/
        public void seMainTankDir() {
            //坦克该移动了
            if (!bU && !bD && !bR && !bL) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (bL) {
                    myTank.setDir(Dir.LEFT);
                }
                if (bR) {
                    myTank.setDir(Dir.RIGHT);
                }
                if (bU) {
                    myTank.setDir(Dir.UP);
                }
                if (bD) {
                    myTank.setDir(Dir.DOWN);
                }
            }
        }

    }

}
